(function() {
	var app = angular.module('cacheMonitorApp', [ 'ui.bootstrap' ]);
	app.controller('cacheMonitorController', function($http, $scope, $log,
			$uibModal,$sce) {
		var controller = this;
		controller.parameter = {};
		controller.queryParameter = "";
		this.chacheList = [];
		$scope.serviceInfo = {};
		$scope.radioval = "";
		this.queryCondition = {
			"system" : "",
			"host" : "",
			"page" : 1,
			"size" : 4,
		};
		this.paginationInfo = {
			"maxSize" : 5,
			"totalPages" : 0,
			"totalElements" : 0
		};

		this.search = function() {
			loadData();
		}

		// 重置事件
		this.reset = function() {
			this.queryCondition = {
					"system" : "",
					"host" : "",
					"page" : 1,
					"size" : 5,
				};
		}
		
		$scope.clickEvent = function() {
			controller.queryCondition.page = controller.queryCondition.forwordPage;
			loadData();
		}

		// 跳转页数，回车事件
		$scope.enterEvent = function(e) {
			var keycode = window.event ? e.keyCode : e.which;
			if (keycode == 13) {
				$scope.clickEvent();
			}
		}

		this.pageChanged = function() {
			loadData();
		}
		// 程序初期化
		this.init = function() {
//			getHostMonitorTopologyDiagram("tuopu");
//			$scope.url = $sce.trustAsResourceUrl('www.baidu.com');
//			$('#hostmonitorDiv').addClass('height: auto');
//			$('#maincontainer').addClass('overflow: hidden');
//			$('.wrapChart3').css('display: none');
			loadData();
		}
		// 程序初期化
		this.init();
		
		//开始
		// 选择主机弹窗  
		// 选择消息中间件弹窗  
		this.selectCacheZTreeHtml = function(data) {
			
			var modalInstanceAdd = $uibModal.open({
						backdrop : 'static',
						keyboard : false,
						scope : $scope,
						templateUrl : 'js/controller/view.display/hostMonitorZTree.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
						controller : 'cacheZTreeController',// 这是模态框的控制器,是用来控制模态框的
						ariaLabelledBy : 'modal-title',
						ariaDescribedBy : 'modal-body',
					});

			modalInstanceAdd.opened.then(function(){//模态窗口打开之后执行的函数  
				//获取树状图数据
				getTreeData("tuopu");
        
			}); 
		}
		
		// 获取树状图数据
		function getTreeData(paramater) {
			var basePath = $scope.getAppBasePath();
			$.ajax({
				type : "POST",
				url : basePath + "/get/queryViewDisplayZtree",
				dataType : "json",
				data:{
					"monitorCenterHostInfomationType":"2"     
				}, 
			/*	0和999 主机监控
				1     分布式数据库监控
				2     消息中间件
				3     分布式缓存监控
				4 oracle监控      */
				success : function(data) {
					var resultArrary = JSON.parse(data);

					var setting = {
						callback : {
//							beforeAsync: zTreeBeforeAsync,
							onClick : zTreeOnClick,
							onExpand: zTreeOnExpand//点击节点前+号加载
						}
					};
					function zTreeBeforeAsync(treeId, treeNode) {
					    return (treeNode.isParent == true);
					};
					// 返回数据转成json
					function ajaxDataFilter(treeId, parentNode, responseData) {
						responseData = JSON.parse(responseData);
						return responseData;
					}
					;
					var zTreeObj = $.fn.zTree.init($("#tuopu"), setting,
							resultArrary);
					$("#search-bt").click(filter);
					//默认点击展开根目录
					$("#tuopu_1_switch").click();
					function zTreeOnExpand(event, treeId, treeNode){
						var nodes = zTreeObj.getSelectedNodes();
						if (nodes.length>0 && treeNode.level != 0 && treeNode.level != 1) {
							zTreeObj.reAsyncChildNodes(nodes[0], "refresh");
							}
					}
					function zTreeOnClick(event, treeId, treeNode) {
						var te = treeNode.sytemParameterId;
						var serchId=document.getElementById("serchConfirm");
						serchId.setAttribute("value",te);
						if(treeNode.isParent == false){
							$scope.serviceInfo.sytemParameterId  = treeNode.sytemParameterId;
							
							//显示是主机名
							$("#selectDiv").html(treeNode.name);
							//关闭弹窗
							$("#hosttreecancel").click();
						}
					}
					
					//过滤ztree显示数据
					var hiddenNodes;
					function filter(){
						//显示上次搜索后背隐藏的结点
						zTreeObj.showNodes(hiddenNodes);
						var _keywords=$("#keyword").val();
						if(_keywords == null || _keywords ==""){
							zTreeObj.showNodes(hiddenNodes);
						}else{
							var nodes = zTreeObj.getNodesByParamFuzzy("name", _keywords, null);  //根据节点数据的属性搜索，获取条件模糊匹配的节点数据 JSON 对象集合,(keywords)模糊匹配只能针对 String 类型的数据
							if (nodes.length>0) { 
								zTreeObj.selectNode(nodes[0]); //会让节点自动滚到到可视区域内
							} 

							//查找不符合条件的叶子节点
							function filterFunc(node){
								var _keywords=$("#keyword").val();
								if(node.isParent||node.name.indexOf(_keywords)!=-1){
									return false;
								}else{
									return true;	
								}
							};

						}
					};
					//弹窗title显示
				$("#modalTitle").html("选择消息中间件");

				}
			});
}
		
		//开始
		this.showResult=function (){
				$scope.url = '';
				var serchId=document.getElementById("serchConfirm");
				$scope.serviceInfo.sytemParameterId  = serchId.getAttribute("value");
				var basePath = $scope.getAppBasePath();
				$.ajax({
					type : "POST",
					url : basePath
							+ "/get/getGrafanaUrl",
					dataType : "json",
					contentType : "application/json",
					data : JSON
							.stringify($scope.serviceInfo.sytemParameterId),
					success : function(data) {
						$scope.url = $sce.trustAsResourceUrl(data);
						$scope.$apply(); 
					},
				error : function(XMLHttpRequest, textStatus){
					$scope.url = $sce.trustAsResourceUrl('about:blank');
					$scope.$apply(); 
				}
				});
				
			}
		
		//结束
		
		//radio选中
		 $scope.redioChecked = function(dat){
		       //alert($scope.radioval);
			 var basePath = $scope.getAppBasePath();
				$scope.url = '';
				
				$.ajax({
					type : "POST",
					url : basePath
							+ "/data/getCacheGrafanaUrl",
					dataType : "json",
					contentType : "application/json",
					data : JSON
							.stringify(dat.systemParameterId),
					success : function(data) {
						$scope.url = $sce.trustAsResourceUrl(data);
						for(var i=0;i<controller.chacheList.length;i++){
							if(dat.systemName == controller.chacheList[i].systemName){
								controller.chacheList[i].checked = true;
							}else{
								controller.chacheList[i].checked = false;
							}
						} 
						$scope.$apply(); 
					},
				error : function(XMLHttpRequest, textStatus){
					$scope.url = $sce.trustAsResourceUrl('www.baidu.com');
					$scope.$apply(); 
				}
				});
		    }
		// 加载数据
		function loadData() {
			if ($(".check_item:checked").length == 0) {
				$("#check_all").prop("checked", false);
			}
			
			var basePath = $scope.getAppBasePath();
			// 参数获取
			controller.parameter.systemName = controller.queryCondition.system ||"";
			controller.parameter.hostName = controller.queryCondition.host ||"";
			controller.parameter.page = controller.queryCondition.page;
			controller.parameter.size = controller.queryCondition.size;
			
			// 后台调用
			 $.ajax({
			 type : "POST",
			 url : basePath + "/data/getCacheInfo",
			 dataType : "json",
			 contentType : "application/json",
			 data : JSON.stringify(controller.parameter),
			 success : function(data) {
			 var jsonResult = angular.fromJson(data);
			 controller.chacheList = jsonResult.list;
			 controller.paginationInfo.totalPages = jsonResult.pages;
			 controller.paginationInfo.totalElements = jsonResult.total;
			 //手动刷新
			 $scope.$apply();  
			 }
			 });

		}
		
	})
	//ztree弹窗js
	.controller(
			'cacheZTreeController',
			function($scope, $uibModalInstance, $http, $log,$filter) {
				//关闭事件
				$scope.cancel = function() {
					// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
					$uibModalInstance.dismiss('cancel');
				};
			});
})();